package palmtop.com.palmtop.activity;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.TextUtils;
import android.widget.Button;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.beardedhen.androidbootstrap.BootstrapEditText;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.model.Personpos;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.ToastUtil;

public class MapActivity extends BaseActivity {

    @BindView(R.id.mapViews)
    MapView mapView;

    AMap aMap = null;
    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    String role_id;
    String ssgrid;
    @BindView(R.id.username)
    BootstrapEditText username;
    @BindView(R.id.selectBtn)
    Button selectBtn;

    ArrayList<Personpos> temlist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map, Config.toptitle, 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(MapActivity.this);
        ButterKnife.bind(this);
        initView(savedInstanceState);


    }

    @OnClick(R.id.selectBtn)
    public void onViewClicked() {
        if(TextUtils.isEmpty(username.getText())){
            ToastUtil.show(MapActivity.this,"请输入姓名");
            return;
        }
        for (int i = 0; i < temlist.size(); i++) {
            if(!(temlist.get(i).getUsername()==null||temlist.get(i).getUsername().equals(""))&&temlist.get(i).getUsername().equals(username.getText().toString())){
                LatLng ln1 = new LatLng(Double.valueOf(temlist.get(i).getLatitude()), Double.valueOf(temlist.get(i).getLongitude()));
                CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(ln1, 16, 0, 0));
                aMap.moveCamera(cameraUpdate);
                break;
            }
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    private void initView(Bundle savedInstanceState) {
        //mapView.onCreate(savedInstanceState);
        mapView.onCreate(savedInstanceState);
        aMap = mapView.getMap();
        AppPreferences appPreferences = new AppPreferences(MapActivity.this);
        role_id = appPreferences.getString("role_id", "");
        ssgrid = appPreferences.getString("ssgrid", "");

        MyLocationStyle myLocationStyle;
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
//        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示，非必需设置。
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。


        RequestParams params = new RequestParams(Config.SERVER_URL + "personpos/getAll");
        if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
            params.addBodyParameter("ssgridid", ssgrid);
        }

        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type listType = new TypeToken<ArrayList<Personpos>>() {
                }.getType();
                ArrayList<Personpos> list = gson.fromJson(result, listType);
                temlist = list;
                Personpos personpos = new Personpos();
                for (int i = 0; i < list.size(); i++) {
                    personpos = list.get(i);
                    LatLng ln1 = new LatLng(Double.valueOf(personpos.getLatitude()), Double.valueOf(personpos.getLongitude()));
                    Marker marker1 = aMap.addMarker(new MarkerOptions().position(ln1).title(personpos.getUsername()));
                    Date d = null;
                    try {
                        d = sDateFormat.parse(personpos.getLogintime());
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
//                     sDateFormat.parse().parse().getTime()/1000;
                    long t = d.getTime() / 1000;
                    if (System.currentTimeMillis() / 1000 - t > 300) {
                        marker1.setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                                .decodeResource(getResources(), R.mipmap.makeronline)));
                    } else {
                        marker1.setIcon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                                .decodeResource(getResources(), R.mipmap.makeronline)));
                    }


//                    if (i == 0) {
//                        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(ln1, 14, 0, 0));
//                        aMap.moveCamera(cameraUpdate);
//                    }
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });




        /*LatLng ln1 = new LatLng(Double.valueOf("39.90114808823611"),Double.valueOf("116.41713157296182"));
        LatLng ln2 = new LatLng(Double.valueOf("39.89985586380674"),Double.valueOf("116.37424305081369"));
        LatLng ln3 = new LatLng(Double.valueOf("39.911371851899325"),Double.valueOf("116.39602258801462"));
        LatLng ln4 = new LatLng(Double.valueOf("39.92786985777907"),Double.valueOf("116.35428071022035"));
        LatLng ln5 = new LatLng(Double.valueOf("39.96298995838631"),Double.valueOf("116.3917753100395"));
        LatLng ln6 = new LatLng(Double.valueOf("39.930328789930854"),Double.valueOf("116.41686871647835"));
        LatLng ln7 = new LatLng(Double.valueOf("39.913501182020276"),Double.valueOf("116.36595636606218"));

        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(ln1, 12, 0, 0));
        aMap.moveCamera(cameraUpdate);

        Marker marker1 = aMap.addMarker(new MarkerOptions().position(ln1).title("网格员1"));
        Marker marker2 = aMap.addMarker(new MarkerOptions().position(ln2).title("网格员1"));
        Marker marker3 = aMap.addMarker(new MarkerOptions().position(ln3).title("网格员1"));
        Marker marker4 = aMap.addMarker(new MarkerOptions().position(ln4).title("网格员1"));
        Marker marker5 = aMap.addMarker(new MarkerOptions().position(ln5).title("网格员1"));
        Marker marker6 = aMap.addMarker(new MarkerOptions().position(ln6).title("网格员1"));
        Marker marker7 = aMap.addMarker(new MarkerOptions().position(ln7).title("网格员1"));



*/
    }


}
